﻿Public Class PrintTextItem
    Inherits PrintItem

    Public Stretch As Boolean
    Public Total As Boolean
    Public Gray As Integer = 0
    Public Link As String
    Public Alignment As String
    Public VerticalAlignment As String
    Public Borders As String
    Public Bold As Boolean
    Public Italic As Boolean
    Public Narrow As Boolean
    Public Wrap As Boolean
    Public Text As String
    Public Title As Boolean
    Public Active As Boolean

	Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics, Path As Drawing2D.GraphicsPath)
		G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
		G.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
		Text = Trim(Text)
		Dim R As New Rectangle(x1 + Indent.Left, y1 + Indent.Top, x2 - x1 - Indent.Horizontal, y2 - y1 - Indent.Vertical)
		Dim GrayR As New Rectangle(x1, y1, x2 - x1, y2 - y1)
		'G.FillRectangle(Brushes.White, GrayR)
		If Title Or Total Or Gray > 0 Then
			If Gray > 0 Then
				Dim c As Integer = Choose(Gray, 192, 224, 240, 255)
				G.FillRectangle(New SolidBrush(Color.FromArgb(c, c, c)), GrayR)
				G.DrawRectangle(Pens.White, GrayR.Left, GrayR.Top, GrayR.Width, GrayR.Height)
			ElseIf Not Total Then
				G.FillRectangle(Brushes.LightGray, GrayR)
				G.DrawRectangle(Pens.White, GrayR.Left, GrayR.Top, GrayR.Width, GrayR.Height)
			Else
				G.FillRectangle(Brushes.LightGray, GrayR)
			End If
		End If
		For i As Integer = 1 To Len(Borders)
			Select Case Mid(Borders, i, 1)
				Case "l" : G.DrawLine(LinePen, x1, y1, x1, y2)
				Case "r" : G.DrawLine(LinePen, x2, y1, x2, y2)
				Case "t" : G.DrawLine(LinePen, x1, y1, x2, y1)
				Case "b" : G.DrawLine(LinePen, x1, y2, x2, y2)
				Case "f" : Path.AddRectangle(New Rectangle(x1, y1, x2 - x1, y2 - y1))
			End Select
		Next
		Dim Style As FontStyle = FontStyle.Regular
		If Bold Then Style += FontStyle.Bold
		If Italic Then Style += FontStyle.Italic
		If TextHeight > 0 Then
			Dim Font As New Font(CStr(If(Narrow, ReportFontNarrow, ReportFontRegular)), TextHeight, Style, GraphicsUnit.Point)
			Dim TextColor = Color.Black
			If Link <> "" And ForScreen Then TextColor = If(Active, Color.Red, Color.Blue)
			If Hidden Then TextColor = Color.LightGray
			If Gray = 3 Then TextColor = Color.DarkGray
			If Gray = 4 Then TextColor = Color.Blue
			Dim StrFormat As New StringFormat
			Select Case Alignment
				Case "right", "r" : StrFormat.Alignment = StringAlignment.Far
				Case "center", "c" : StrFormat.Alignment = StringAlignment.Center
				Case Else : StrFormat.Alignment = StringAlignment.Near
			End Select
			StrFormat.LineAlignment = StringAlignment.Center
			Select Case VerticalAlignment
				Case "bottom", "b" : StrFormat.LineAlignment = StringAlignment.Far
				Case "top", "t" : StrFormat.LineAlignment = StringAlignment.Near
				Case Else : StrFormat.LineAlignment = StringAlignment.Center
			End Select
			Dim TextToPrint As String = Text
			If TextToPrint = "[-]" Then TextToPrint = "-"
			If TextToPrint = "[x]" Then TextToPrint = "û" : Font = New Font("wingdings", Font.Size, Font.Unit)
			If TextToPrint = "[/]" Then TextToPrint = "ü" : Font = New Font("wingdings", Font.Size, Font.Unit)
			If TextToPrint = "[ ]" Then TextToPrint = "o" : Font = New Font("wingdings", Font.Size, Font.Unit)
			If ForScreen AndAlso (Title Or Total Or Gray > 0) Then
				G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
				DrawStringLighted(G, TextToPrint, Font, TextColor, R, StrFormat)
			Else
				G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
				If Fit Then
					Do
						Dim w = G.MeasureString(TextToPrint, Font).Width
						If w <= R.Width Then Exit Do
						Font = New Font(Font.Name, Font.Size - 1, Font.Style)
					Loop
				End If
				DrawText(G, TextToPrint, Font, TextColor, R, StrFormat)
				'DrawText2(G, Text, Font, TextBrush.Color, Rectangle.Round(R), StrFormat)
			End If
		End If
			'G.DrawRectangle(Pens.Red, R)
	End Sub

    Function Contains(ByVal Point As Point) As Boolean
        Return Region.Contains(Point)
    End Function

    ReadOnly Property Region() As Rectangle
        Get
            Return New Rectangle(x1, y1, x2 - x1, y2 - y1)
        End Get
    End Property

End Class